package com.cjl.dental_clinic.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjl.dental_clinic.Req.LoginReq;
import com.cjl.dental_clinic.base.ResVo;
import com.cjl.dental_clinic.entity.User;
import com.cjl.dental_clinic.Req.UserReq;
import com.cjl.dental_clinic.mapper.UserMapper;
import com.cjl.dental_clinic.service.UserService;
import com.cjl.dental_clinic.util.JWTUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: cjl
 * @time: 2024/01/14 18:18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public ResVo<String> register(LoginReq loginReq) {
        // 判断用户是否存在，如果存在就返回让用户登录，不存在就注册
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        long userCount = count(userQueryWrapper.eq("username", loginReq.getUsername()));
        if (userCount > 0) {
            return ResVo.error("用户已存在");
        }
        User user = new User();
        user.initCreateUser();
        user.setUsername(loginReq.getUsername()).setPassword(new BCryptPasswordEncoder().encode(loginReq.getPassword()));
        user.setLevel(1);
        saveOrUpdate(user);
        return ResVo.ok("注册成功");
    }

    @Override
    public ResVo Login(LoginReq loginReq) {
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        User user = baseMapper.selectOne(Wrapper.eq("username", loginReq.getUsername()));
        if (user == null) return ResVo.error("用户不存在");
        if (user.getStatus()==1) return ResVo.error("用户被禁用");
        // 生成token
        if (new BCryptPasswordEncoder().matches(loginReq.getPassword(), user.getPassword())) {
            HashMap<String, String> map = new HashMap<>();
            map.put("username", user.getUsername());
            map.put("userId", String.valueOf(user.getId()));
            String token = JWTUtils.getToken(map);
            map.put("level",user.getLevel().toString());
            map.put("token",token);
            return ResVo.ok(map);
        }
        return ResVo.error("密码错误");
    }

    @Override
    public List<UserReq> selectNoPassword() {
        return baseMapper.selectNoPassword();
    }

    @Override
    public Boolean userStatus(Integer id) {
        User user = baseMapper.selectById(id);
        user = user.getStatus() == 1 ? user.setStatus(0) : user.setStatus(1);
        return baseMapper.updateById(user)>0;
    }

    @Override
    public Boolean modify(LoginReq user) {
        User one = baseMapper.selectOne(new QueryWrapper<User>().eq("username", user.getUsername()).eq("is_del", 0));
        if (ObjectUtil.isEmpty(one)){
            return false;
        }
        one.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        return updateById(one);
    }
}
